<?php
/*
	Copyright 2006, 2007, 2008, 2009, 2010 Bastiaan Grutters
    
    This file is part of Ages of Strife website.

    Ages of Strife website is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    Ages of Strife website is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with Ages of Strife website.  If not, see <http://www.gnu.org/licenses/>.
 */
include( '../global/initialize.php' );

$query = "SELECT * " .
		"FROM army " .
		"WHERE ruler_id = " . $_SESSION[ 'ruler_id' ];
$result = mysql_query( $query ) or die( "Query failed : " . mysql_error() );
$num = mysql_numrows( $result );
$i = 0;
$army_ids = array();
while ( $i < $num ) {
	if( isset( $_POST[ mysql_result( $result, $i, "army_id" ) ] ) ) {
		$army_ids[] = mysql_result( $result, $i, "army_id" );
	}
	$i ++;
}
if( count( $army_ids ) == 0 ) {
	$_SESSION[ 'start_mission_status' ] = translate( "No army selected" );
	header( "Location: operations.php" );
}
else {
	aos_header( 'New mission' );

	$query = "SELECT * FROM ruler WHERE ruler_id = " . $_SESSION[ 'ruler_id' ];
	$result = mysql_query( $query ) or die( "Query failed : " . mysql_error() );
	$row = mysql_fetch_array($result, MYSQL_ASSOC);
	$gold = $row[ 'gold' ];

	include( "../global/resources.php" );
	if( count( $army_ids ) > 1 ) {
		print( "<div class=\"blok\">\r\n" );
		print "<br />" . translate( 'No multiple army missions available yet.' ) . "<br />\r\n";
		print( "</div><br />\r\n" );
	}
	else {
		$query = "SELECT * " .
				"FROM army " .
				"WHERE army_id = " . $army_ids[0];
		$result = mysql_query( $query ) or die( "Query failed : " . mysql_error() );
		$row = mysql_fetch_array($result, MYSQL_ASSOC);
		if( $row[ 'scout' ] + $row[ 'infantry' ] + $row[ 'cavalry' ] + $row[ 'artillery' ] == 0 ) {
?>
					<div class="blok">
						<?php transprint( 'You can\'t send empty armies on a mission.' ); ?>
					</div>
<?php			
		}
		elseif( $row[ 'follow' ] > 0 ) {
?>
					<div class="blok">
						<?php transprint( 'This army is following another army.' ); ?>
					</div>
<?php					
		}
		elseif( $row[ 'training' ] > 0 ) {
?>
					<div class="blok">
						<?php transprint( 'This army is currently transferring.' ); ?>
					</div>
<?php					
		}
		elseif( $row[ 'mission' ] > 0 ) {
?>
					<div class="blok">
						<?php transprint( 'This army is already on a mission.' ); ?>
					</div>
<?php					
		}
		else {
			print "<br /><h2>" . $row[ 'name' ] . "</h2>\r\n";
			print "<form method=\"post\" action=\"start_mission2.php\" >\r\n";
		    print "<table><th colspan=\"4\">" . translate( 'Missions' ) . "</th>\r\n";
	    	print "<tr>" .
	    			"<td>" . translate( 'Select' ) . "</td>" .
	    			"<td>" . translate( 'Name' ) . "</td>" .
	    			"<td>" . translate( 'Gold cost' ) . "</td>" .
	    			"<td>" . translate( 'Duration' ) . "</td>" .
	    			"</tr>\r\n";
	    	if( $gold > 20 && $row[ 'fortified' ] == 0 ) {
	    		print "<tr class=\"even\"><td><input type=\"radio\" control=\"mission_radio\" value=\"0\" name=\"mission_radio\" /></td><td>" . translate( 'Small scouting mission' ) . "</td><td><strong class=\"gold\">20</strong></td><td>5 " . translate( 'days' ) . "</td></tr>\r\n";
	    	}
	    	if( $gold > 40 && $row[ 'fortified' ] == 0 ) {
	    		print "<tr><td><input type=\"radio\" control=\"mission_radio\" value=\"1\" name=\"mission_radio\" /></td><td>" . translate( 'Medium scouting mission' ) . "</td><td><strong class=\"gold\">40</strong></td><td>10 " . translate( 'days' ) . "</td></tr>\r\n";
	    	}
	    	if( $gold > 80 && $row[ 'fortified' ] == 0 ) {
	    		print "<tr class=\"even\"><td><input type=\"radio\" control=\"mission_radio\" value=\"2\" name=\"mission_radio\" /></td><td>" . translate( 'Large scouting mission' ) . "</td><td><strong class=\"gold\">80</strong></td><td>15 " . translate( 'days' ) . "</td></tr>\r\n";
	    	}
	    	if( $gold > 160 && $row[ 'fortified' ] == 0 ) {
	    		print "<tr><td><input type=\"radio\" control=\"mission_radio\" value=\"3\" name=\"mission_radio\" /></td><td>" . translate( 'Very large scouting mission' ) . "</td><td><strong class=\"gold\">160</strong></td><td>20 " . translate( 'days' ) . "</td></tr>\r\n";
	    	}
	    	if( $gold > 200 && $row[ 'fortified' ] == 0 ) {
	    		print "<tr class=\"even\"><td><input type=\"radio\" control=\"mission_radio\" value=\"4\" name=\"mission_radio\" /></td><td>" . translate( 'Fortify on and bombard from current position' ) . "</td><td><strong class=\"gold\">200</strong></td><td>8 " . translate( 'days' ) . "</td></tr>\r\n";
	    	}
	    	else if( $gold > 80 && $row[ 'fortified' ] == 1 ) {
	    		print "<tr class=\"even\"><td><input type=\"radio\" control=\"mission_radio\" value=\"5\" name=\"mission_radio\" /></td><td>" . translate( 'Dig out of fortified position' ) . "</td><td><strong class=\"gold\">80</strong></td><td>4 " . translate( 'days' ) . "</td></tr>\r\n";
	    	}
			print "<tr><td><input type=\"hidden\" name=\"army_id\" value=\"$army_ids[0]\" /></td><td><input type=\"submit\" value=\"" . translate( 'Start mission' ) . "\"/></td><td></td><td></td></tr>";
			print "</table></form>\r\n";
		}
	}
	aos_footer();
}
?>